
Option Explicit On
Imports System.Diagnostics
Imports System.IO
''' <summary>
''' this class allows to decompile a CHM file into html
''' 
''' </summary>
''' <remarks>if makes use of hh.exe</remarks>
Public Class clsDecompile
    Private Const ghil As String = Chr(34)
    ''' <summary>
    ''' decompile a chm file
    ''' </summary>
    ''' <param name="CHMFile">the name of the file</param>
    ''' <returns>the hhc decompiled file</returns>
    ''' <remarks>uses the <see cref="DecompileChm"></see> function by putting the dir of the chm file</remarks>
    Public Function DecompileChm(ByVal CHMFile As String) As String
        Dim pathDir As String = Path.GetDirectoryName(CHMFile)
        pathDir = Path.Combine(pathDir, Path.GetFileNameWithoutExtension(CHMFile))
        Return DecompileChm(CHMFile, pathDir)
    End Function
    ''' <summary>
    ''' decompile a chm file
    ''' </summary>
    ''' <param name="CHMFile">the name of the file</param>
    ''' <param name="FolderToPut">where to put files</param>
    ''' <returns>the hhc decompiled file</returns>
    ''' <remarks>uses hh.exe</remarks>
    Public Function DecompileChm(ByVal CHMFile As String, ByRef FolderToPut As String) As String
        If (Not System.IO.File.Exists(CHMFile)) Then
            Throw New ArgumentException("file does not exists" + CHMFile)
        End If
        If (Not Directory.Exists(FolderToPut)) Then
            FolderToPut = FolderToPut.Replace(" ", "_")
            Directory.CreateDirectory(FolderToPut)
        End If
        Dim di As DirectoryInfo = New DirectoryInfo(FolderToPut)
        If (di.Name.Contains(" ")) Then
            Throw New ArgumentException("Foler to decompile can not contain spaces")
        End If
        Dim strD As String
        strD = " -decompile " & di.FullName & " " & CHMFile
        Console.WriteLine(strD)
        Dim p As Process = Process.Start("hh.exe", strD)

        p.WaitForExit()
        Return Directory.GetFiles(FolderToPut, "*.HHC")(0)
    End Function



End Class
